package com.bcu.team.comment.service.serviceImpl;

import com.bcu.team.comment.entity.Collection;
import com.bcu.team.comment.entity.Message;
import com.bcu.team.comment.mapper.CollectionMapper;
import com.bcu.team.comment.mapper.SongMapper;
import com.bcu.team.comment.mapper.UserMapper;
import com.bcu.team.comment.service.CollectionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CollectionServiceImpl implements CollectionService {

    @Resource
    private CollectionMapper collectionMapper;
    @Resource
    private SongMapper songMapper;
    @Resource
    private UserMapper userMapper;

    @Override
    public List<Collection> getCollectionsByUserId(Integer userId) {
        return collectionMapper.getCollectionsByUserId(userId);
    }

    @Override
    public Message addCollection(Collection collection) {

        if(songMapper.selectByPrimaryKey(collection.getCollectionSongId())==null){
            return Message.fail("该歌曲id不存在");
        }
        if (userMapper.selectByPrimaryKey(collection.getCollectionUserId())==null){
            return Message.fail("请确认用户id");
        }

        if(collectionMapper.isUserCollectedSong(collection.getCollectionSongId(),collection.getCollectionUserId())!=null){
            return Message.fail("该用户已经收藏了这首歌");
        }else {
            return collectionMapper.insertSelective(collection) > 0 ? Message.success("添加成功") : Message.fail("添加失败");
        }
    }

    @Override
    public boolean isUserCollectedSong(Integer songId, Integer userId) {
        if(collectionMapper.isUserCollectedSong(songId,userId) !=null){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Message delayCollectedSong(Integer songId, Integer userId) {
        if(collectionMapper.isUserCollectedSong(songId,userId) !=null){
            return collectionMapper.deleteBySongIdAndUserId(songId,userId) > 0 ? Message.success("删除成功") : Message.fail("删除失败");
        }else {
            return Message.fail("该用户没有收藏该歌曲");
        }
    }
}
